//
//  PBMainTBC.m
//  PBMain
//
//  Created by 曹连华 on 2020/4/7.
//

#import "PBMainTBC.h"
#import <CoreCode/UIImage+instance.h>
#import <PBHome/PBHome.h>
#import <PBAccount/PBAccount.h>

//修复iOS 12.1系统，push再pop时，tabbarButton位置错误。
//参考 https://github.com/ChenYilong/iOS12AdaptationTips/issues/3
@implementation UITabBar (position)
CG_INLINE BOOL
OverrideImplementation(Class targetClass, SEL targetSelector, id (^implementationBlock)(Class originClass, SEL originCMD, IMP originIMP)) {
    Method originMethod = class_getInstanceMethod(targetClass, targetSelector);
    if (!originMethod) {
        return NO;
    }
    IMP originIMP = method_getImplementation(originMethod);
    method_setImplementation(originMethod, imp_implementationWithBlock(implementationBlock(targetClass, targetSelector, originIMP)));
    return YES;
}

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (@available(iOS 12.1, *)) {
            OverrideImplementation(NSClassFromString(@"UITabBarButton"), @selector(setFrame:), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP originIMP) {
                return ^(UIView *selfObject, CGRect firstArgv) {
                    if ([selfObject isKindOfClass:originClass]) {
                        // 如果发现即将要设置一个 size 为空的 frame，则屏蔽掉本次设置
                        if (!CGRectIsEmpty(selfObject.frame) && CGRectIsEmpty(firstArgv)) {
                            return;
                        }
                    }
                    // call super
                    void (*originSelectorIMP)(id, SEL, CGRect);
                    originSelectorIMP = (void (*)(id, SEL, CGRect))originIMP;
                    originSelectorIMP(selfObject, originCMD, firstArgv);
                };
            });
        }
    });
}
@end

@interface PBMainTBC ()

@end

@implementation PBMainTBC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    
}

- (void)initUI {
    BaseNC *homeNC = [[BaseNC alloc]initWithRootViewController:[HomeVC new]];
    UITabBarItem *homeBarItem = [[UITabBarItem alloc]initWithTitle:@"首页" image:[[UIImage  imageWithFileName:@"tab_home_normal" bundleName:@"PBHomeResources" aClass:self.class]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageWithFileName:@"tab_home_selected" bundleName:@"PBHomeResources" aClass:self.class]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [homeBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateSelected];
    [homeBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateNormal];
    homeNC.tabBarItem = homeBarItem;
    
    BaseNC *accountNC = [[BaseNC alloc]initWithRootViewController:[PBAccountVC new]];
    UITabBarItem *accountBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:[[UIImage  imageWithFileName:@"tab_account_normal" bundleName:@"PBAccountResources" aClass:self.class]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageWithFileName:@"tab_account_selected" bundleName:@"PBAccountResources" aClass:self.class]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [accountBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateSelected];
    [accountBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateNormal];
    accountNC.tabBarItem = accountBarItem;
    
    
    [self setViewControllers:@[homeNC,accountNC]];
    
}
- (UIImage *)getTabImageWithName:(NSString *)imageName {
    // 注意图片名称的格式，必须写全 @2x 或者 @3x  后缀名都得写
    // 获取当前的bundle,self只是在当前pod库中的一个类，也可以随意写一个其他的类
    NSBundle *currentBundle = [NSBundle bundleForClass:[self class]];
    // 获取当前bundle的名称
    // 获取图片的路径
    NSString *imagePath = [currentBundle pathForResource:imageName ofType:nil inDirectory:@"PBMainResources.bundle"];
    // 获取图片
    UIImage *imageFile = [UIImage imageWithContentsOfFile:imagePath];
    return imageFile;
}
@end
